package com.tcp2.ui;

import com.tcp2.server.DataExchangeServer;
import com.tcp2.server.DxsConfig;
import com.tcp2.util.ConfigUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import javax.swing.event.TableColumnModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputMethodEvent;
import java.awt.event.InputMethodListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyVetoException;
import java.beans.VetoableChangeListener;
import java.util.Enumeration;

/**
 * Author: <黄书根>
 * Time: 2017-08-09 10:50
 * Copyright (C) 2017 Xiamen Yaxon Networks CO.,LTD.
 */
public class MainFrame {
    private static Logger logger = LoggerFactory.getLogger(MainFrame.class);
    public JPanel MainPanel;
    private JTextField textField1;
    private JTextField textField2;
    private JTextField textField3;
    private JTextField textField4;
    private JButton Start_Button;
    private JButton Stop_Button;
    private JButton Restart_Button;
    private JTabbedPane Log_Tab;
    private JPanel TopPanel;
    private JPanel MidPanel;
    private JPanel BottomPanel;
    private JPanel LeftPanel;
    private JPanel RightPanel;
    private JPanel Top_Left_Panel;
    private JPanel Top_Right_Panel;
    private JPanel Right_Top_Panel;
    private JPanel Right_Bot_Panel;
    private JPanel Right_Mid_Panel;
    private JPanel Log_Tab_1;
    private JPanel Log_Tab_3;
    private JProgressBar progressBar1;
    private JPanel Log_Tab_2;
    private JButton button1;
    private JButton button2;
    private JButton button3;
    private JButton button4;
    private JButton button5;
    private JButton button6;
    private JTable table1;
    private JTextField textField5;
    private JPanel Left_Top_Panel;
    private JPanel Left_Top_Panel_1;
    private JPanel Left_Top_Panel_2;
    private JProgressBar masterPro;
    private JProgressBar salvePro;
    private JScrollBar table2;
    private DefaultTableModel model;
    public static String[] str;
    private DataExchangeServer app;

    public static boolean ServerStatus = false;
    public static boolean MasterStatus = false;
    public static boolean SlaveStatus = false;

    public MainFrame() {
        InitData();
/*
* 服务操作 3按钮监听
* */
        Start_Button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                //  model.addRow(new String[] { "asd", "asdsa"});
                try {
                    if (progressBar1.getForeground().equals(Color.GREEN)) {
                        return;
                    }

                    SaveDate();
                    app = new DataExchangeServer();
                    app.run(str);//

                } catch (Throwable e) {
                    //logger.error("", e);
                    // System.exit(3);
                    try {
                        app.start();
                    } catch (Throwable e1) {

                    }
                }

            }
        });
        Stop_Button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                try {
                    app.stop();

                } catch (Throwable e) {
                    logger.error("", e);
                }
            }
        });


        textField4.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                String old = textField4.getText();
                if (!old.equals("")) {
                    try {
                        int a = Integer.parseInt(old);
                        textField4.setText(a + "");
                    } catch (Exception e) {
                        textField4.setText("0");
                    }
                }
            }
        });
        Restart_Button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                Stop_Button.doClick();
                Start_Button.doClick();
            }
        });
        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                //  System.out.println(MasterStatus);
            }
        });
    }


    /*进度条*/
    public void ProgressBarUp(JProgressBar p) {
        p.setForeground(Color.GREEN);

    }

    public void ProgressBarDown(JProgressBar p) {
        p.setForeground(Color.RED);

    }

    public void ProStatus(JProgressBar p, Boolean status) {
        if (status) {
            ProgressBarUp(p);
        } else {
            ProgressBarDown(p);
        }
    }

    /*
    * 初始化页面的数据
    * */
    public void InitData() {
        textField1.setText(ConfigUtil.getFileIO("link.gnssCenterId", "app.properties"));
        textField2.setText(ConfigUtil.getFileIO("link.username", "app.properties"));
        textField3.setText(ConfigUtil.getFileIO("link.pwd", "app.properties"));
        textField4.setText(ConfigUtil.getFileIO("dxs.bind.port", "app.properties"));
        textField5.setText(ConfigUtil.getFileIO("link.ip", "app.properties"));
//===============================
        String[][] datas = {};
        String[] titles = {"日志来源", "日志描述", "日期", "消息ID", "原始数据"};
        model = new DefaultTableModel(datas, titles);
        JTable table = new JTable(model);
        Log_Tab_1.add(new JScrollPane(table));


        Thread t = new Thread(new Runnable() {
            public void run() {
                while (true) {
                    ProStatus(progressBar1, ServerStatus);
                    if (!ServerStatus) {
                        ProStatus(masterPro, ServerStatus);
                        ProStatus(salvePro, ServerStatus);
                    } else {
                        ProStatus(masterPro, MasterStatus);
                        ProStatus(salvePro, SlaveStatus);
                    }
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        t.start();

    }

    /*
    * 保存页面上的配置
    * */
    public void SaveDate() {
        ConfigUtil.writeData("link.gnssCenterId", textField1.getText(), "app.properties");
        ConfigUtil.writeData("link.username", textField2.getText(), "app.properties");
        ConfigUtil.writeData("link.pwd", textField3.getText(), "app.properties");
        ConfigUtil.writeData("dxs.bind.port", textField4.getText(), "app.properties");
        ConfigUtil.writeData("link.ip", textField5.getText(), "app.properties");


    }

    public static void main(String[] args) {
        str = args;
        MainFrame mf = new MainFrame();

        JFrame frame = new JFrame("MainFrame");
        frame.setContentPane(mf.MainPanel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);


    }


}
